/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.facade;

import java.io.File;
import org.apache.ivy.Ivy;
import org.apache.ivy.core.cache.DefaultRepositoryCacheManager;
import org.apache.ivy.core.cache.RepositoryCacheManager;
import org.apache.ivy.core.module.descriptor.Artifact;
import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
import org.apache.ivy.core.report.ArtifactDownloadReport;
import org.apache.ivy.core.resolve.DownloadOptions;

/**
 *
 * @author lforet
 * @author xhanin
 */
public class ArtifactInfo {

    private Ivy ivy;
    private ModuleDescriptor descriptor;
    private ArtifactDownloadReport report;

    private File javadocFile;
    private File sourceFile;
    
    public ArtifactInfo(Ivy ivy, ModuleDescriptor descriptor, ArtifactDownloadReport report) {
        this.ivy = ivy;
        this.descriptor = descriptor;
        this.report = report;

        if (descriptor != null) {
            Artifact[] artifacts = descriptor.getAllArtifacts();
            for (Artifact artifact : artifacts) {
                if (artifact.getName().equals(report.getArtifact().getName())) {
                    if ("source".equals(artifact.getType())) {
                        sourceFile = provision(artifact);
                    } else if ("javadoc".equals(artifact.getType())) {
                        javadocFile = provision(artifact);
                    }
                }
            }
        }
    }

    private File provision(Artifact metaArtifact) {
        RepositoryCacheManager cache = ivy.getSettings()
                .getResolver(metaArtifact.getModuleRevisionId()).getRepositoryCacheManager();
        if (cache instanceof DefaultRepositoryCacheManager) {
            File metaArtifactFile = ((DefaultRepositoryCacheManager) cache)
                    .getArchiveFileInCache(metaArtifact);
            if (metaArtifactFile.exists()) {
                return metaArtifactFile;
            } else {
                ivy.getResolveEngine().download(metaArtifact, new DownloadOptions());
                if (metaArtifactFile.exists()) {
                    return metaArtifactFile;
                }
                return null;
            }
        }
        ArtifactDownloadReport metaAdr = ivy.getResolveEngine().download(
                metaArtifact, new DownloadOptions());
        if (metaAdr.getLocalFile() != null && metaAdr.getLocalFile().exists()) {
            return metaAdr.getLocalFile();
        }
        return null;
    }

    public String getName() {
        return report.getName();
    }

    public File getJarFile() {
        return report.getLocalFile();
    }

    public File getSourceFile() {
        return sourceFile;
    }

    public File getJavadocFile() {
        return javadocFile;
    }
    
    public String getVersion() {
        return report.getArtifact().getModuleRevisionId().getRevision();
    }

    public String getOrganisation() {
        return report.getArtifact().getModuleRevisionId().getOrganisation();
    }

    public boolean hasSource() {
        return getSourceFile() != null;
    }

    public boolean hasJavadoc() {
        return getJavadocFile() != null;
    }

    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer(super.toString()).append("[")
                .append(getName()).append(", ")
                .append(getVersion()).append(", ")
                .append(hasJavadoc()).append(", ")
                .append(hasSource()).append("]");
        return buffer.toString();
    }

    
    
}
